// pages/signup/signup.js
import {formatTimeYMD} from "../../utils/util.js"
import { requestD } from "../../request/request.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '',
    date: '',
    today: formatTimeYMD(new Date()),
    cindex: 0,
    cnameList:[
      "健康社区"
    ]
  },

  commList: [
    {
      cid: 1,
      cname: "健康社区",
      formAddr: "湖北省武汉市洪山区",
      business: "软件圆中路"
    }
  ],

  bindDateChange(e){
    this.setData({
      date: e.detail.value
    })
  },

  bindCommChange(e){
    this.setData({
      cindex: e.detail.value
    })
  },

  formSubmit(e){
    console.log(e)
    wx.showLoading({mask: true})
    let info = e.detail.value
    const tip = this.checkForm(info)
    wx.hideLoading()
    if(tip===''){
      const type = this.data.type
      let reqParam = {...info, type}
      if(type==3){
        reqParam.cid = this.commList[info.cindex].cid
      }
      requestD({
        url:'person/signup',
        data:reqParam
      }).then(result=>{
        const resp = result.data
        console.log(resp)
        if(resp.error_code==0){
          wx.showToast({
            title: '注册成功',
            icon: 'done',
            duration: 800,
            mask: true,
            success: (result)=>{
              setTimeout(function(){
                wx.navigateBack()
              }, 800)
            }
          })
        }else{
          wx.showToast({
            title: '未成功,'+resp.msg,
            icon: 'none',
            duration: 800,
            mask: true
          })
        }
      })

    }else{
      wx.showToast({
        title: tip,
        icon: 'none',
        duration: 800,
        mask: true
      })
    }
  },

  cidRe:/^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
  passRe:/^[a-zA-Z0-9]+$/,
  phoneRe:/^1[3456789]\d{9}$/,
  cnameRe:/^[\u4E00-\u9FA5]{2,4}$/,

  checkForm(info){
    const {phone, password, passConfirm, name, gender, civilId, birthdate} = info
    if(!this.phoneRe.test(phone)){return '手机号错误'}
    if(password.length<6 || password.length>16){return '密码6-16位'}
    if(!this.passRe.test(password)){return '密码只能字母和数字'}
    if(passConfirm!=password){return '两次密码不等'}
    if(!this.cnameRe.test(name)){return '姓名错误'}
    if(!this.cidRe.test(civilId)){return '身份证号错误'}
    if(gender===''){return '请选择性别'}
    if(birthdate===this.data.today){return '请选择生日'}
    return ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      date: this.data.today,
      type: parseInt(options.type)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})